package 多线程;

import java.util.concurrent.CountDownLatch;

public class countDownLatchTest {
    public static void main(String[] args) throws InterruptedException{
        CountDownLatch latch = new CountDownLatch(3);

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "-begin...");
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // count--
            latch.countDown();
            System.out.println(Thread.currentThread().getName() + "-end..." + latch.getCount());
        }).start();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "-begin...");
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // count--
            latch.countDown();
            System.out.println(Thread.currentThread().getName() + "-end..." + latch.getCount());
        }).start();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "-begin...");
            try {
                Thread.sleep(1500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // count--
            latch.countDown();
            System.out.println(Thread.currentThread().getName() + "-end..." + latch.getCount());
        }).start();
        System.out.println(Thread.currentThread().getName() + "-wating...");
        // await
        latch.await();
        System.out.println(Thread.currentThread().getName() + "-wait end...");
    }
}
